﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ResourceBuilder
{
   /// <summary>
   /// Directory Helper class used to locate resources.
   /// </summary>
   public class DirectoryHelper
   {
      #region Fields      
      private string _solutionRootDirectoryPath;      
      private string _assetsDirectoryPath;
      private string _cssAssetsDirectoryPath;
      private string _controlRootDirectoryPath;
      #endregion Fields

      #region Properties
      /// <summary>
      /// Gets the assets directory path.
      /// </summary>
      /// <value>The assets directory path.</value>
      public string AssetsDirectoryPath
      {
         get
         {
            if (string.IsNullOrEmpty(this._assetsDirectoryPath))
            {
               this._assetsDirectoryPath = Path.Combine(this.ControlRootDirectoryPath, Settings1.Default.AssetsDirectory);
            }
            return this._assetsDirectoryPath;
         }
      }

      /// <summary>
      /// Gets the CSS assets directory path.
      /// </summary>
      /// <value>The CSS assets directory path.</value>
      public string CssAssetsDirectoryPath
      {
         get
         {
            if (string.IsNullOrEmpty(this._cssAssetsDirectoryPath))
            {
               this._cssAssetsDirectoryPath = Path.Combine(this.AssetsDirectoryPath, Settings1.Default.CssDirectory);
            }
            return this._cssAssetsDirectoryPath;
         }
      }

      /// <summary>
      /// Gets the control root directory path.
      /// </summary>
      /// <value>The control root directory path.</value>
      public string ControlRootDirectoryPath
      {
         get
         {
            if (string.IsNullOrEmpty(this._controlRootDirectoryPath))
            {
               this._controlRootDirectoryPath = Path.Combine(this.SolutionRootDirectoryPath, Settings1.Default.RootControlDirectory);
            }
            return this._controlRootDirectoryPath;
         }
      }

      /// <summary>
      /// Gets the solution root directory path.
      /// </summary>
      /// <value>The solution root directory path.</value>
      public string SolutionRootDirectoryPath
      {
         get
         {
            if (string.IsNullOrEmpty(this._solutionRootDirectoryPath))
            {
               this._solutionRootDirectoryPath = this.GetParentDirectory(this.CurrentDirectoryPath, Settings1.Default.RootSolutionDirectory);
            }
            return this._solutionRootDirectoryPath;
         }
      }

      /// <summary>
      /// Gets the current directory path.
      /// </summary>
      /// <value>The current directory path.</value>
      public string CurrentDirectoryPath
      {
         get { return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); }
      }

      #endregion Properties

      #region Methods
      /// <summary>
      /// Gets a parent directory from the current directory.
      /// </summary>
      /// <param name="currentDirectory">The current directory.</param>
      /// <param name="lookingForDirectoryName">Name of the looking for directory.</param>
      /// <returns></returns>
      private string GetParentDirectory(string currentDirectory, string lookingForDirectoryName)
      {
         if (String.Compare(Directory.CreateDirectory(currentDirectory).Name, lookingForDirectoryName, true) == 0)
            return currentDirectory;
         else
            return GetParentDirectory(Directory.GetParent(currentDirectory).FullName, lookingForDirectoryName);
      }

      /// <summary>
      /// Creates the directory.
      /// </summary>
      /// <param name="fullPath">The full path.</param>
      public void CreateDirectory(string fullPath)
      {
         if (!Directory.Exists(fullPath))
            Directory.CreateDirectory(fullPath);
      }
      #endregion Methods

      #region Ctors
      #endregion Ctors
   }
}
